`include "chunjun_define.sv" 
`include "chunjun_lib_define.sv" 
 
//==============================================================
// Date        : 2024-10-16
// Description :
//==============================================================

module wing_cbb_plru_decode #(
    parameter WAY_N  = 4
) (
    input  logic[WAY_N-1:0] lock_way_i,
    input  logic[WAY_N-2:0] code_i,
    output logic[WAY_N-1:0] rplc_way_o
);

logic [WAY_N-2:0] code_temp;
logic [WAY_N-1:0][$clog2(WAY_N)-1:0] related_bit;

// code re-generation
for (genvar i=0; i<$clog2(WAY_N); i++) begin: g_level
 
    localparam X = (i==0) ? 0 : ((1<<i)-1);

    for (genvar j=0; j<(2**i); j++) begin: g_node
        assign code_temp[X+j] = (& lock_way_i[(WAY_N/(2**i))*j +: (WAY_N/(2**(i+1)))])                        ? 1'b1 :    
                                (& lock_way_i[((WAY_N/(2**i))*j + (WAY_N/(2**(i+1)))) +: (WAY_N/(2**(i+1)))]) ? 1'b0 : code_i[X+j];    
    end
 
end

// related bit generation
for (genvar a=0; a<WAY_N; a++) begin: g_way_bitmap
 
    for (genvar b=0; b<$clog2(WAY_N); b++) begin: g_related_bit_assign   

        localparam B = $clog2(WAY_N) - 1 -b;
        localparam K = (B==0) ? 0 : ((1<<B)-1);
        //localparam K = 1;
        localparam Z = (a/(WAY_N/(2**B))) + K;

        assign related_bit[a][b] = code_temp[Z];
    end
 
end

// rplc_way generation
for (genvar c=0; c<WAY_N; c++) begin: g_rplc_way
 
    assign rplc_way_o[c] = (related_bit[c] == c);
 
end


endmodule

`include "chunjun_undefine.sv" 
